﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using hzGis.service.impl;
using hzGis.service;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Framework;

namespace hzGis
{
    public partial class PrintWin : Form
    {
        private static PrintService printService = new PrintService();
        private static IFileService fileService = new FileService();
        public PrintWin()
        {
            InitializeComponent();
            this.loadDefaultShp();
        }

        private void axPageLayoutControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnMouseDownEvent e)
        {

        }

        private void loadBtn_Click(object sender, EventArgs e)
        {
            
        }

        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button == 4)
                axMapControl1.Pan();
        }

        // 当mapview数据加载变化时触发的事件
        private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            Console.WriteLine("数据发生变化one");
            //CopyToPageLayout();
        }

        //axMapControl 中的数据显示状况发生变化时，需要联动
        private void axMapControl1_OnViewRefreshed(object sender, IMapControlEvents2_OnViewRefreshedEvent e)
        {
            Console.WriteLine("数据发生变化two");
            //CopyToPageLayout();
        }

        private void axMapControl1_OnAfterScreenDraw(object sender, IMapControlEvents2_OnAfterScreenDrawEvent e)
        {
            IActiveView pActiveView = (IActiveView)axPageLayoutControl1.ActiveView.FocusMap;
            IDisplayTransformation displayTransformation = pActiveView.ScreenDisplay.DisplayTransformation;
            displayTransformation.VisibleBounds = axMapControl1.Extent;
            axPageLayoutControl1.ActiveView.Refresh();
            CopyToPageLayout();
        }

        // map加载默认shp
        private void loadDefaultShp()
        {
            fileService.openShpFile("D:\\data\\QMDLTB2022.shp", this.axMapControl1);
            this.axMapControl1.Map.MapScale = 2000;
            IPoint center = new ESRI.ArcGIS.Geometry.Point();
            center.X = 554679.55;
            center.Y = 2529012.945;
            this.axMapControl1.CenterAt(center);
            
        }

        private void loadMxd()
        {
            axMapControl1.LoadMxFile("D:\\data\\tempP.mxd");
        }

        //地图复制函数
        private void CopyToPageLayout()
        {
            IObjectCopy pObjectCopy = new ObjectCopy();
            object copyFromMap = axMapControl1.Map;
            object copiedMap = pObjectCopy.Copy(copyFromMap);//复制地图到copiedMap中
            object copyToMap = axPageLayoutControl1.ActiveView.FocusMap;
            pObjectCopy.Overwrite(copiedMap, ref copyToMap); //复制地图
            setPageLayerOut(copyToMap as IMap);
            axPageLayoutControl1.ActiveView.Refresh();
        }

        // 设置pageLayerout的样式
        private void setPageLayerOut(IMap pMap)
        {
            printService.addTitle("赣深高铁惠州北站片区防洪排涝工程（一期）惠城段项目2018年度土地利用现状图（局部4-1）", this.axPageLayoutControl1, 0, 0.5, 29.2 * 2 + 0.5, 20.5 * 3.87, 30, new byte[]{0, 0, 0}, "title");
            IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)axPageLayoutControl1.PageLayout;
            // 设置默认边框
            ISymbolBorder pSymborder = new SymbolBorder() as ISymbolBorder;
            pSymborder.CornerRounding = 0;
            IBorder pBorder = pSymborder;
            // 设置外框
            IEnvelope pEnvelop_out = new Envelope() as IEnvelope;
            // 设置外框的大小
            pEnvelop_out.PutCoords(2, 2, 29.5 * 2 - 2, 20.6 * 2 - 2);
            // 添加外框
            IFrameElement fe = new FrameElement() as IFrameElement;
            fe.Border = pBorder;
            IElement pe = (IElement)fe;
            pe.Geometry = pEnvelop_out as IGeometry;
            pGraphicsContainer.AddElement(pe, 0);
            // 设置地图的大小
            IMapFrame pMapFrame = (IMapFrame)pGraphicsContainer.FindFrame(pMap);
            pMapFrame.Border = pBorder;
            pMapFrame.ExtentType = esriExtentTypeEnum.esriAutoExtentBounds;
            IElement pElement = (IElement)pMapFrame;
            IEnvelope pEnvelop = new Envelope() as IEnvelope;
            pEnvelop.PutCoords(2.3, 2.3, 29.5 * 2 - 2.3, 20.6 * 2 - 2.3);  //这里设置mapframe的大小
            IGeometry pGeometry = pEnvelop;
            pElement.Geometry = pGeometry;
            // 添加指北针
            printService.AddNorthArrow(this.axPageLayoutControl1.PageLayout, pMap, 29.5 * 2 - 5, 20.6 * 2 - 7, 29.5 * 2 - 4, 20.6 * 2 - 4, "esriCarto.MarkerNorthArrow");
            // 添加图例图片
            printService.addPicture(this.axPageLayoutControl1, 2.3, 2.3, 11.8, 11.3, "D:\\data\\tl.png");
            // 添加备注
            printService.addRectangle(this.axPageLayoutControl1, 2.3, 12.3, 17.8, 13.5, new byte[] { 23, 145, 255 }, 1, new byte[] { 185, 250, 255 });
            printService.addTitle("绿色范围图斑为1999年的建设用地范围，面积为0.0119公顷", this.axPageLayoutControl1, 3, 12.3, 17, 13, 16, new byte[]{0, 0, 0}, "info");
            //这里设置page的大小
            IPage pPage = axPageLayoutControl1.Page;
            pPage.Orientation = 1;
            pPage.PutCustomSize(29.7 * 2, 21.0 * 2);  
        }

        private void axPageLayoutControl1_OnMouseDown_1(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
        {

        }

        private void printBtn_Click(object sender, EventArgs e)
        {
            PrintPage();
        }

        /// <summary>
        /// 打印地图
        /// </summary>
        public void PrintPage()
        {
            IActiveView pActiveView = this.axPageLayoutControl1.ActiveView;
            try
            {
                PrintDialog printDialog = new PrintDialog();
                printDialog.AllowPrintToFile = false;
                System.Drawing.Printing.PrintDocument pPrintDocument = new System.Drawing.Printing.PrintDocument();
                printDialog.Document = pPrintDocument;
                DialogResult result = printDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    //获取打印份数
                    int Copies = pPrintDocument.PrinterSettings.Copies;
                    //获取打印方向
                    bool landscape = pPrintDocument.DefaultPageSettings.Landscape;
                    //获取打印分辨率
                    int ResolutionX = pPrintDocument.DefaultPageSettings.PrinterResolution.X;
                    int ResolutionY = pPrintDocument.DefaultPageSettings.PrinterResolution.Y;


                    IPaper pPaper = new Paper();
                    
                    //设置打印机名称
                    pPaper.PrinterName = pPrintDocument.PrinterSettings.PrinterName;
                    //设置打印方向
                    if (landscape)
                    {
                        pPaper.Orientation = 2;
                    }
                    else
                    {
                        pPaper.Orientation = 1;
                    }

                    IPrinter pPrinter = new EmfPrinter() as IPrinter;

                    ///如果不设置打印名称的话发送到实体打印机打印时会报错
                    pPrinter.SpoolFileName = "地图打印";
                    pPrinter.Paper = pPaper;

                    //设置打印分辨率
                    ESRI.ArcGIS.esriSystem.tagRECT usertagRECT = pActiveView.ExportFrame;
                    usertagRECT.left = 0;
                    usertagRECT.top = 0;
                    usertagRECT.right = (int)(pPrinter.PrintableBounds.Width * ResolutionX);
                    usertagRECT.bottom = (int)(pPrinter.PrintableBounds.Height * ResolutionY);
                    IEnvelope pDriverBounds = new Envelope() as IEnvelope;
                    pDriverBounds.PutCoords(usertagRECT.left, usertagRECT.top, usertagRECT.right, usertagRECT.bottom);

                    //开始打印
                    pActiveView.Output(pPrinter.StartPrinting(pDriverBounds, 0), pPrinter.Resolution, ref usertagRECT, null, null);
                    pPrinter.FinishPrinting();

                    MessageBox.Show("地图打印成功!", "提示", MessageBoxButtons.OK);

                }
                else
                {
                   // return new Response(Constant.Nothing, "");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("打印失败！" + ex.Message + ex.Source);
            }

           // return new Response(Constant.Success, "");
        }

        private void axPageLayoutControl1_OnMouseDown_2(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
        {

        }

        private void styleBtn_Click(object sender, EventArgs e)
        {
           
        }


    }
}
